<?php

function seo_baiviet($id,$str1)
{
//return BASE_URL.$id."_".$str1.".htm";
return "index.php?module=news&act=view&id=".$id;
}





function seo_category($id,$str1)
{
//return BASE_URL."c".$id."/".trim($str1)."/";
return "index.php?module=news&act=list&cat=".$id;
}



function seo_page($id,$str1)
{
//return BASE_URL."p".$id."/".trim($str1)."/";
return "index.php?module=news&act=list&cat=".$id;
}



function seo_subcategory($id,$str2)
{
//return BASE_URL."s".$id."/".$str2."/";
return "index.php?module=news&act=list&sub=".$id;

}

function category_info($catid){
	$cache=new cache;
	$cat_info=$cache->load("cat".$catid);

	
	if(!$cat_info)
	{
		$mnews=new Model_News;
		$mnews->get_catbyid($catid);
		$row=$mnews->fetchOne();
		$row['url']=seo_category($row['cat_id'],$row['cat_alias']);
		$mnews->query("select * from news_subcategory where category=".$row['cat_id']." order by thutu asc");
	
		if($mnews->num_rows()>0){
					$datasub=$mnews->fetchAll();
					$datasub2=array();
					foreach($datasub as $rs){
					$rs1=$rs;
					$rs1['url']=seo_subcategory($rs['sub_id'],$rs['sub_alias']);
					$datasub2[]=$rs1;
					}
					$row['sub']=$datasub2;
		}
		
		if($row)
		{
			$cache->save("cat".$catid,$row,10000);
			
			$cat_info=$row;
			return $cat_info;
		}
		else
		{
	return false;
		}
		
	}
	return $cat_info;

}

function subcategory_info($sub){
	$cache=new cache;
	$sub_info=$cache->load("sub".$sub);
	if(!$sub_info)
	{
		$mnews=new Model_News;
		$mnews->get_subbyid($sub);
		$row=$mnews->fetchOne();
		if($row)
		{
			$row['url']=seo_subcategory($row['sub_id'],$row['sub_alias']);
			$cache->save("sub".$sub,$row,10000);
			return $row;
		}
		else
		{
			return false;
		}
		
	}
	return $sub_info;
	
}

function category($where){
	$db=new db;
	$db->connect();
	$db->query("select * from category  where  ".$where." order by  thutu asc");
	$data=$db->fetchAll();
	return $data;
}
        
        
  function createTagCloud()  
{     
    $db=new db;
	$db->connect();
	$db->query("select * from tag");
    $total_tuts =$db->num_rows();  
	$tags=$db->fetchAll();
    
      

      
    //ugh, XHTML in PHP?  Slap my hands - this isn't best practice, but I was obviously feeling lazy  
    $html = '<div class="tagcloud">';  
    
    //iterate through each item in the $output array (created above)  
    foreach($tags as $tag)  
    {  
        //get the number-of-tag-occurances as a percentage of the overall number  
        $ratio = (100 / $total_tuts) * $tag['num'];  
          
        //round the number to the nearest 10  
        $ratio =  round($ratio,-1);  
          
        /*append that classname onto the list-item, so if the result was 20%, it comes out as cloud-20*/  
        $html.= '<a  class="tag-link" href=http://localhost/xgrid/tag/'.khongdau($tag['tag']).'/ title="'.$tag['tag'].'">'.$tag['tag'].'</a>';           
    }  
      
    //close the UL  
    $html.= '</div>';  
    $db->close();
    return $html;   
}   

function tagscloud(){
    $cache=new cache;
	$tagcloud=$cache->load("tagscloud");
	if(!$tagcloud){
		$tagcloud=createTagCloud();
		$cache->save("tagscloud",$tagcloud,1000);
	}
	return $tagcloud;
}


